This report if from BONUS Lab in R number 8 - Temporal Series II with Montecarlo Simulations

We will apply Montecarlo Simulations and Efficient Portfolio Management theory to Financial Time Series to determine how much of 5 Global eCommerce stocks we should buy for our investment portfolio. Namely, we will be looking at:

Mercado Libre (Latam), Amazon and Ebay (USA/Europe), Alibaba & JD.com (Asia/China)


Technical charts in more detail

Source: Yahoo Finance + All things R.


Correlation of stock returns, fx rates, market index log returns, and risk free rates.

Individual assets and the market move in correlation and covariance with many additional financial and economical factors, each with its respective volatilities. I run a wide analysis on the mayor indexes and other possible influential players for each of the companies, countries and mayor markets where these companies operate.

As we can see from the graph below, as expected, the mayor influences on these eCommerce companies performance are: each other, of course, the market indexes of the US, the index for the markets in their countries of origin, and the indexes of the mayor markets they operate in.

All companies under our current coverage are traded in the US stock markets. Beyond this first introductory analysis and going forward, for our purposes of risk management and portfolio management, we will work with three key economic factors:

  1. The US short term 13 week T bill;
  2. the US long term 10 year bond; and
  3. The S&P 500 value.

Notes: “X” marks denote a pairwise correlation rejected at a 0.05 sig.level.
Sources of variables: Yahoo Finance.


Capital Asset Pricing Model per company

Lets begin by looking at how our individual stock prices and performance are tied to the US market. We will use historical returns starting on January of 2015 for MELI, AMZN, EBAY, BABA and JD.

Looking at daily arithmetic returns for each stock and the S&P500 minus the daily closing interest rate of the 10 year US treasury Bond, which we use as the risky free return, we compute each stock’s and the S&P500’s excess return. We then compare each of their excess returns versus the excess return of the market, represented by the S&P 500 index.

With alphas very close to zero, all stocks are fairly priced.

MELI AMZN EBAY BABA JD
Beta 1.23 0.94 0.82 0.93 1.09
Alpha 0.00339 0.000868 -0.000124 -0.000238 0.00149
Over/Under Priced Under-priced Neutral Over-priced Over-priced Under-priced
Market Risk Exposure 27.54% 34.04% 5.27% 29.01% 24.97%
Expected Excess Returns -0.15% 0.09% 0.19% 0.1% -0.03%

Decomposing MELI: Trend and Seasonality

Decompose trend and seasonal factros with Seasonal Decomposition of Time Series by Loess

##  Call:
##  stl(x = meli.ts[, "MELI.Adjusted"], s.window = "periodic", robust = T)
## 
##  Time.series components:
##     seasonal             trend            remainder         
##  Min.   :-43.47121   Min.   :115.8062   Min.   :-193.73181  
##  1st Qu.: -8.57296   1st Qu.:130.9593   1st Qu.: -18.12678  
##  Median :  2.00026   Median :278.0939   Median :  -3.03494  
##  Mean   :  0.65807   Mean   :304.4755   Mean   :  -0.70110  
##  3rd Qu.: 11.46687   3rd Qu.:434.6018   3rd Qu.:  17.51607  
##  Max.   : 62.55654   Max.   :648.6298   Max.   : 196.31552  
##  IQR:
##      STL.seasonal STL.trend STL.remainder data  
##       20.04       303.64     35.64        237.54
##    %   8.4        127.8      15.0         100.0 
## 
##  Weights:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.0000  0.8133  0.9452  0.8141  0.9888  1.0000 
## 
##  Other components: List of 5
##  $ win  : Named num [1:3] 13661 379 253
##  $ deg  : Named int [1:3] 0 1 1
##  $ jump : Named num [1:3] 1367 38 26
##  $ inner: int 1
##  $ outer: int 15

Projections for MELI

Note: I will set seed 123 at the very begining so that everyone’s randomizer can be set equally to reproduce the same or as close to the same results at any time the same data is used.

Forecast using STL (Seasonal Decomposition of Time Series by Loess)

1 year is approximately 252 business/trading days, so we will forecast 252 periods ahead.

There are 3 similar methods (stl, stlm, and stlf), we will use stl.

Forecast using ETS (Exponential smoothing state space model)

COMPARE ACCURACY with Diebold-Mariano test for predictive accuracy

Usage

dm.test(e1, e2, alternative=c(“two.sided”,“less”,“greater”), h=1, power=1)

Arguments

e1 Forecast errors from method 1 - for our purposes here, STL will be method 1.

e2 Forecast errors from method 2 - here, ETS will be method 2.

The alternative input is a character string specifying the alternative hypothesis, must be one of “two.sided” (default), “greater” or “less”. You can specify just the initial letter.

The forecast horizon used in calculating e1 and e2 is determined with h.

P is the power used in the loss function. Usually 1 or 2. The choice of power is entirely due to the loss function. If we lose x dollars if the forecast error is x - then our loss function is linear and we should use the option power = 1. If we lose x^2 dollars when the forecast error is x, then we should use power = 2.

Details

The null hypothesis is that the two methods have the same forecast accuracy.

For alternative = “two.sided”, the alternative hypothesis is that method 1 and method 2 have different levels of accuracy

For alternative = “less”, the alternative hypothesis is that method 2 is less accurate than method 1.

For alternative = “greater”, the alternative hypothesis is that method 2 is more accurate than method 1.

A smaller p-value means that there is stronger evidence in favor of the alternative hypothesis

STLM vs. ETS

## 
##  Diebold-Mariano Test
## 
## data:  stl.forecast.meli$residualsets.forecast.meli$residuals
## DM = 3.2277, Forecast horizon = 252, Loss function power = 1, p-value =
## 0.001278
## alternative hypothesis: two.sided

p-value is very low, H0 rejected, STL and ETS have different levels of accuracy.

## 
##  Diebold-Mariano Test
## 
## data:  stl.forecast.meli$residualsets.forecast.meli$residuals
## DM = 3.2277, Forecast horizon = 252, Loss function power = 1, p-value =
## 0.0006388
## alternative hypothesis: greater

p-value is very low, H0 rejected: ETS is MORE accurate than STL.

## 
##  Diebold-Mariano Test
## 
## data:  stl.forecast.meli$residualsets.forecast.meli$residuals
## DM = 3.2277, Forecast horizon = 252, Loss function power = 1, p-value =
## 0.9994
## alternative hypothesis: less

p-value = 1, H0 accepted: ETS is NOT less accurate than STL.

So ETS is more accurate than STL in this case.

Montecarlo Simulations for Price Projections

Lets use MONTECARLO simulations now to run 20,000 simulations for each of 252 trading days into the future.

Alternatively to auto.arima which can take very long and be very heavy on computational resources, there is the option to use auto ARFIMA out of the rugarch library.

The Rugarch package is one of the more robust R libraries for time series data analysis and forecasting.

Most importantly, in addition to target values, it allows to forecast and simulate deviations or volatility, the base for VaR (Value at Risk) estimates in portfolio management.

Rugarch can also accept streaming data and build it into the models, but we will not cover that here - it is just nice to know.

Sources: http://www.unstarched.net/r-examples/rugarch/a-short-introduction-to-the-rugarch-package/ https://palomar.home.ece.ust.hk/MAFS6010R_lectures/Rsession_time_series_modeling.html

##   AR MA Mean ARFIMA       AIC converged
## 1  0  0    1      0 -4.539027         1
## 2  0  1    1      0 -4.537928         1
## 3  1  0    1      0 -4.537011         1
## 4  0  1    0      0 -4.536723         1
## 5  0  2    1      0 -4.536519         1
## 6  2  0    1      0 -4.536493         1

So ARMA(0,0) model WITH a mean

Parameters for ARIMA(p,d,q)

If convergence problems arise:

In choosing ARMA(p,q) the theory of difference equations suggests that we should choose ARMA(p+1,p), which gives rise to a solution of the difference equation with p+1 terms. You can of course have some zero orders

This is explained in my book H. D. Vinod, “Hands-On Intermediate Econometrics Using R: Templates for Extending Dozens of Practical Examples.” (2008) World Scientific Publishers: Hackensack, NJ. (http://www.worldscibooks.com/economics/6895.html)

There are no convergence problems here so we will use p and q with/without mean as estimated for each of the stocks we are looking into.

After running the 20,000 simulations for each of 252 trading days into the future, we get the following results:

The 1 year (252 trading days approx.) target price for MERCADO LIBRE is USD 1349.41, (a 55.65% change compared to the last closing price of USD 866.93)

Makes sense!


Projections for AMAZON

The 1 year (252 trading days approx.) target price for AMAZON is USD 3894.01, (a 56.83% change compared to the last closing price of USD 2483)

Seems a little TOO bullish, but we have enough simulations on variation for the VaR that we will do further down.


Projections for EBAY

The 1 year (252 trading days approx.) target price for EBAY is USD 65.69, (a 34.55% change compared to the last closing price of USD 48.82)

Makes sense, also a little too bullish and with a conditional sigma which looks a little too flat - we can double check that with the VaR exercise.


Projections for ALIBABA

The 1 year (252 trading days approx.) target price for ALIBABA is USD 219.93, (a 0.17% change compared to the last closing price of USD 219.55)

Makes sense, seems to have been trading sideways for quite a long time.


Projections for JD.com

The 1 year (252 trading days approx.) target price for JD.com is USD 58.65, (a -0.67% change compared to the last closing price of USD 59.04)

Makes sense, similar to the case of Alibaba!


eCommerce porfolio allocation with and without short selling

The optimal or efficient portfolios that follow were formulated using:
. Projected daily prices based on the simulations for all companies that we discussed earlier.
. Computing arithmetic daily returns of said projected prices.
. Limitation on the maximum portfolio participation of any single stock to not go above a ceiling of 50% when short sales are allowed, and a more conservative maximum allocation of 35% when short sales are not allowed. I chose these constraints for arbitrary reasons.
. Sharpe ratio calculated using stocks daily simple returns in excess of risk free rate. . 10 year Long Term US Treasury Bond as the risk free return.

MELI AMZN EBAY BABA JD
Weights - Shorts Allowed, Max 50% allocation 0.17 0.29 0.50 0.02 0.03
Weights - No Shorts, Max 35% allocation 0.24 0.35 0.35 0.03 0.03


Portfolio and Components’ Value-at-Risk (VaR)

Key points to keep in mind:
. VaR is an approximation measure to the worst that could happen (max losses) on one single day out of twenty (5%).
. With positive correlation, asset prices tend to move together and this increases the volatility.
. Without short sales, it is impossible to go above the expected return of the stock with the highest expected return.
. When short sales are allowed, there is no upper bound on the expected return nor on the risk. . Although not done here, a short sale strategy (and all others for that manner) should be combined with stop-loss limit orders which carries an additional cost.

Total MELI AMZN EBAY BABA JD
VaR(5%) - Short Sales Allowed -14.05% -2.98% -5.18% -5.89% 0% 0.01%
VaR(5%) - Short Sales Prohibited -14.58% -4.21% -6.25% -4.12% 0% 0.01%

References: R Core Team (2015). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. <URL https://www.R-project.org/>. All Things R. Pull Yahoo Finance Key-Statistics Instantaneously Using XML and XPath in R. <URL: http://allthingsr.blogspot.com.ar/2012/10/pull-yahoo-finance-key-statistics.html>. Raymond McTaggart and Gergely Daroczi (2015). Quandl: API Wrapper for Quandl.com. R package version 2.6.0. <URL: http://CRAN.R-project.org/package=Quandl>. Taiyun Wei (2013). corrplot: Visualization of a correlation matrix. R package version 0.73. <URL: http://CRAN.R-project.org/package=corrplot>. Jeffrey A. Ryan (2015). quantmod: Quantitative Financial Modelling Framework. R package version 0.4-5. <URL: http://CRAN.R-project.org/package=quantmod>. Hyndman RJ (2015). forecast: Forecasting functions for time series and linear models. R package version 6.1, <URL: http://github.com/robjhyndman/forecast>. Hyndman RJ and Khandakar Y (2008). “Automatic time series forecasting: the forecast package for R.” Journal of Statistical Software, 26(3), pp. 1-22. <URL: http://ideas.repec.org/a/jss/jstsof/27i03.html>. Alexios Ghalanos (2015). rugarch: Univariate GARCH models. R package version 1.3-6. Andrew Matuszak. the Economist at Large. Efficient Frontier and plotEfficientFrontier. <URL: http://economistatlarge.com/>. David Ruppert. Statistics and Data Analysis for Financial Engineering (Springer Science+Business Media, LLC, 233 Spring Street, New York, NY 10013, USA). John L. Weatherwax, PhD. A Solution Manual for: Statistics and Data Analysis for Financial Engineering by David Rupert. <URL: http://www.waxworksmath.com>. S original by Berwin A. Turlach R port by Andreas Weingessel (2013). quadprog: Functions to solve Quadratic Programming Problems.. R package version 1.5-5. <URL: http://CRAN.R-project.org/package=quadprog>. Brian G. Peterson and Peter Carl (2014). PerformanceAnalytics: Econometric tools for performance and risk analysis. R package version 1.4.3541. <URL: http://CRAN.R-project.org/package=PerformanceAnalytics>. Yihui Xie (2015). knitr: A General-Purpose Package for Dynamic Report Generation in R. R package version 1.11. Yihui Xie (2015) Dynamic Documents with R and knitr. 2nd edition. Chapman and Hall/CRC. ISBN 978-1498716963. Yihui Xie (2014) knitr: A Comprehensive Tool for Reproducible Research in R. In Victoria Stodden, Friedrich Leisch and Roger D. Peng, editors, Implementing Reproducible Computational Research. Chapman and Hall/CRC. ISBN 978-1466561595. JJ Allaire, Jeffrey Horner, Vicent Marti and Natacha Porte (2015). markdown: ‘Markdown’ Rendering for R. R package version 0.7.7. http://CRAN.R-project.org/package=markdown.